/**
 * Created by caiqyxyx on 2017/2/9.
 */
/** 配置require js **/
require.config({
    /** lib库的根目录 **/
    baseUrl: cywork.global.rootPath || "./",
    urlArgs: "dbg=" + (new Date()).getTime(),
    paths: {
        text: "library/3rdparty/require/text-2.0.12",
        domReady: "library/3rdparty/require/domready-2.0.1",
        durandal: "library/3rdparty/durandal/2.1.0/js",
        css: "library/3rdparty/require/css-0.1.2",
        plugins: "library/3rdparty/durandal/2.1.0/js/plugins",
        transitions: "library/3rdparty/durandal/2.1.0/js/transitions",
        knockout: "library/3rdparty/knockout/knockout-3.2.0.min",
        koValidate: "library/3rdparty/knockout/knockout.validation",
        koVCN: "library/3rdparty/knockout/kovalidation-cn",
        koExt: "library/3rdparty/knockout/koextends",
        kovholder: "library/3rdparty/knockout/kovalidationtholder",
        jquery: "library/3rdparty/jquery/jquery/jquery-2.1.4.min",
        selectize: "library/3rdparty/jquery/selectize/selectize-0.11.2.min",
        tree: "library/3rdparty/jquery/easyui-tree/tree-1.4.1",
        ui: "library/3rdparty/jquery/ui/jquery-ui.min",
        panel: "library/cywork/panal/panal",
        panalManager: "library/cywork/panal/panalmanager",
        resizable: "library/3rdparty/jquery/easyui-resizable/resizable-1.4.1",
        core: "library/cywork/utils/core",
        mask: "library/cywork/utils/mask",
        string: "library/cywork/utils/string",
        parser: "library/3rdparty/jquery/easyui-parser/parser-1.4.1",
        cookie: "library/3rdparty/jquery/cookie/jquery.cookie",
        http: "library/cywork/utils/http",
        WdatePicker: "library/3rdparty/My97DatePicker/WdatePicker",
        bootstrap: "library/3rdparty/bootstrap/js/bootstrap.min",
        i18nCommon: "library/cywork/common/i18n",
        bootbox: "library/3rdparty/bootbox/bootbox.min",
        cyworkUI: "library/cywork/utils/MainUiUtils",
        slickGrid: "library/3rdparty/jquery/slickgrid/slick.grid-2.1.0",
        pager: "library/cywork/utils/pager",
        BMap: "http://api.map.baidu.com/getscript?v=2.0&ak=gr0euZ619EAGF0UHilSSF6UTS9AtINze&services=&t=" + (new Date()).getTime(),
        basicUtils: "library/cywork/utils/BasicUtils",
        validate: "library/cywork/utils/validate",
        ReconnectingWebSocket: "library/3rdparty/reconnectingwebsocket/reconnecting-websocket",
        websocket: "library/cywork/utils/websocket",
        video: "library/3rdparty/video6.2.5.js/js/video.min",
        navhelper: "library/cywork/utils/navhelper",
        echart: "library/3rdparty/echart/echarts.min",
        tip:"library/cywork/utils/tip",
        box:"library/cywork/utils/box"
    },
    shim: {
        bootstrap: {
            deps: ['jquery'],
            exports: 'jQuery'
        },
        WdatePicker: {
            exports: "WdatePicker"
        },
        BMap: {
            deps: ['jquery'],
            exports: 'BMap'
        },
        slickGrid: {
            deps: ["jquery",
                "library/3rdparty/jquery/slickgrid/jquery.event.drag-2.2",
                "library/3rdparty/jquery/slickgrid/jquery.event.drop-2.2",
                "library/3rdparty/jquery/slickgrid/slick.core",
                "library/3rdparty/jquery/slickgrid/slick.formatters",
                "library/3rdparty/jquery/slickgrid/slick.editors",
                "library/3rdparty/jquery/slickgrid/plugins/slick.autotooltips",
                "library/3rdparty/jquery/slickgrid/plugins/slick.rowselectionmodel",
                "library/3rdparty/jquery/slickgrid/plugins/slick.cellselectionmodel",
                "library/3rdparty/jquery/slickgrid/plugins/slick.cellrangedecorator",
                "library/3rdparty/jquery/slickgrid/plugins/slick.cellrangeselector",
                "library/3rdparty/jquery/slickgrid/plugins/slick.checkboxselectcolumn",
                "library/3rdparty/jquery/slickgrid/plugins/slick.radioselectcolumn",
                "library/3rdparty/jquery/ui/jquery-ui.js",
                "css!library/3rdparty/jquery/slickgrid/slick.grid-2.1.0",
                "css!library/3rdparty/jquery/slickgrid/smoothness/jquery-ui-1.8.16.custom",
                "css!library/3rdparty/jquery/slickgrid/slick-default-theme"
            ],
            exports: "Slick"
        },
        tree: {
            deps: ["jquery", "ui", "parser", "css!library/3rdparty/jquery/easyui-tree/tree-1.4.1"],
            exports: "$.fn.tree"
        },
        parser: {
            deps: ["jquery"],
            exports: "$.fn.parser"
        }
    }
});

/** 全局错误处理 **/
requirejs.onError = function (error) {
    console.log(error);
};

define(['i18nCommon', 'durandal/system', 'durandal/app', 'jquery', 'http', 'box', 'cookie', 'knockout'], function (i18n, system, app, $, http, box, cookie, ko) {

    //>>excludeStart("build", true);
    system.debug(true);
    //>>excludeEnd("build");

    //alert(window.location.host);

    app.title = i18n.textSystemTitle;

    app.configurePlugins({
        router: true,
        dialog: true
    });

    /*扩展knockout*/
    ko.observableArray.fn.pushAll = function (valuesToPush) {
        var underlyingArray = this();
        this.valueWillMutate();
        ko.utils.arrayPushAll(underlyingArray, valuesToPush);
        this.valueHasMutated();
        return this;
    };


    /**
     * Sets the root module/view for the application.
     * @method setRoot
     * @param {string} root The root view or module.
     * @param {string} [transition] The transition to use from the previous root (or splash screen) into the new root.
     * @param {string} [applicationHost] The application host element or id. By default the id 'applicationHost' will be used.
     */
    app.start().then(function () {
        var validateSession = http.getInstance("main/validateSession");
        validateSession.ajax().then(function (data, result) {
            if (data.sessionValid) {
                cywork.global.humanId = data.human.humanId;
                cywork.global.humanName = data.human.humanName;
                cywork.global.unitName = data.human.unitName;
                $.cookie('human_id_cookie', data.human.humanId);
                $.cookie('human_name_cookie', data.human.humanName);
                app.setRoot('view/law/law', 'entrance');
            } else {
                app.setRoot(i18n.loginPage, 'entrance');
            }
        }).error(function () {
            box.alert("系统出错,请联系管理员!");
        });
    });
});